<?php

/*

  Action - Attack	$5

  Each other player reveals cards from the top of his deck until revealing one costing $3 or more.
  He trashes that card and may gain a card costing at most $2 less than it.
  He discards the other revealed cards.

 */

class Saboteur extends Card implements iAction, iKingdom, iAttack, iIntrigue {

    public function __construct() {
        parent::__construct("Saboteur", 5);
        $this->undoPlay = false;
        $this->undoBuy = true;
    }

    public function getDescription() {
        return array(
            "Each other player reveals cards from the top of his deck until revealing one costing $3 or more.  He trashes that card and may gain a card costing at most $2 less than it.  He discards the other revealed cards.");
    }

    public function play(Game $game, $isRealCard) {

        $game->setPhase(Phase_Sleep);
        $game->attack($this);

        $names = $game->getOtherPlayersNames(true);
        foreach ($names as $name) {
            $card = $game->removeCardFromDrawDeck($name);
            $temp_discard = array();

            while ($card != null) {
                $coins = $card->getPrice($game)->getCoins();
                if ($coins >= 3) {
                    break;
                } else {
                    $temp_discard[] = $card;
                }
                $card = $game->removeCardFromDrawDeck($name);
            }

            //discarding the low value cards
            $game->discardTempDiscardCards($temp_discard, $name);

            ScreenDisplay::addScreenMessage($game, true, "discard " . nicePrint($temp_discard), "discards " . ScreenDisplay::$LMSG, $name);
            //now we trash the found card and present the other player some options
            if ($card != null) {
                $game->putCardInTrash($card, $name);
                ScreenDisplay::addScreenMessage($game, false, ", trash " . nicePrint($card), ", trashes " . ScreenDisplay::$LMSG, $name);
                $price = $card->getPrice($game);
                $all_possible_options = $game->getBoardCardsByPrice(new Price(), $price->add(-2));
                $all_possible_options[] = new Option(1, "None");


                $output = MySocket::GetUserReply($game, Input_ChooseOne, $all_possible_options, 1, 1, null, "Your " . $card->getValue() . " was trashed, please choose a new card", $name, true);
            }
        }

        //the waiting function
        $users_replies = MySocket::getUsersReplies($game);
        foreach ($users_replies as $name => $output) {

            $card_name = $output[0];
            if ($card_name != 'None') {
                $card = $game->moveCardFromPileToPile($card_name, BoardCards, DiscardPile, $name);
                ScreenDisplay::addScreenMessage($game, false, " and gain " . nicePrint($card), " and gains " . ScreenDisplay::$LMSG, $name);
            } else {
                ScreenDisplay::addScreenMessage($game, false, " and gain nothing", " and gains nothing", $name);
            }

            $game->setPhase(Phase_Sleep, $name);
        }

        $game->cardActionFinished();
        return true;
    }

}

?>